<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="借入单">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">
    </style>

    <script>
        jQuery(function () {
            fixTableOperatorButton();
            ajaxSelects("${baseUrl}user/user/getAll.action.action",[
                {selectName:"userId",head:"全部" },
                {selectName:"makeUserId",head:"全部" }]);
            ajaxSelect("${baseUrl}organization/store/getStores.action","storeId","全部");

            ajaxSelect("${baseUrl}organization/house/listNormalVo.action","houseId","全部");
        });

        layui.use(['form', 'table','laydate'], function () {
            let $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                laydate = layui.laydate,
                util = layui.util;

            laydate.render({
                elem: '#date',
                range:'~',
                done: function(value, date, endDate){
                    if (!emptyEntity(value)) {
                        $('[name="date1"]').val(getDateFromLayUiDate(date));
                        $('[name="date2"]').val(getDateFromLayUiDate(endDate));
                    } else {
                        $('[name="date1"]').val('');
                        $('[name="date2"]').val('');
                    }
                }
            });

            laydate.render({
                elem: '#makeTime',
                range:'~',
                done: function(value, date, endDate){
                    if (!emptyEntity(value)) {
                        $('[name="makeTime1"]').val(getDateFromLayUiDate(date));
                        $('[name="makeTime2"]').val(getDateFromLayUiDate(endDate));
                    } else {
                        $('[name="makeTime1"]').val('');
                        $('[name="makeTime2"]').val('');
                    }
                }
            });


            renderTable();


            table.on('tool(table)', function (obj) {
                let data = obj.data;
                switch(obj.event){
                    case 'close':
                        close(data);
                        break;
                    case 'detail':
                        openTab("借入单详情","${baseUrl}house/borrowInDetail.action?id="+data.id);
                        break;
                    case 'back':
                        openTab("新增借入归还单","${baseUrl}house/borrowInBackNewByBorrowIn.action?borrowInId="+data.id);
                        break;
                    case 'inGoods':
                        openTab("新增转进货单","${baseUrl}inGoods/inGoodsNewByBorrowIn.action?borrowInId="+data.id);
                        break;
                }
            });

            table.on('toolbar(table)', function (obj) {
                let data = table.checkStatus('table').data;
                switch(obj.event){
                    case 'add':
                        openTab("新增借入单","${baseUrl}house/borrowInNew.action");
                        break;
                    case 'export':
                        exportExcel(data);
                        break;
                };
            });

            form.on('checkbox(noClose)', function (data) {
                table.reload('table',{where: genSearch(),page: {curr: 1}});
            });

            form.on('submit(search)', function (data) {
                table.reload('table',{where: genSearch(),page: {curr: 1}});
                return false;
            });

        });

        function genSearchClose(noClose) {
            if (isTrue(noClose)) {
                return false;
            } else {
                return "";
            }
        }

        function clearSearch() {
            $("#searchForm")[0].reset();
            $('[name="date1"]').val('');
            $('[name="date2"]').val('');
            $('[name="makeTime1"]').val('');
            $('[name="makeTime2"]').val('');
        }

        function renderTable() {
            clearSearch();
            layui.table.render({
                id:'table',
                elem: '#table',
                toolbar: '#tableToolBar',
                defaultToolbar:[],
                url: "${baseUrl}house/borrowIn/getList.action",
                totalRow:true,
                toolbar: '#tableToolBar',
                defaultToolbar:[],
                where : {
                    close :false,
                    time:(new Date()).valueOf()
                },
                cols: [[
                    {width:70,title:"序号",templet:function (d) {
                        return genTableIndex(d.close, "作废",d);
                        }},
                    {width: 100, title: '业务日期',style:"font-size:12px;",templet: function (d) {
                            return GMTToDate(d.date);
                        }},
                    {width: 100, title: '经手人',style:"font-size:12px;",templet: function (d) {
                            return d.user == null ? "" :d.user.name;
                        }},
                    {field: 'no', width: 130, title: '单据编号',style:"font-size:12px;"},
                    {width: 120, title: '供应商',style:"font-size:12px;", templet: function (d) {
                            return d.provider == null ? "" :d.provider.name;
                        }},
                    {minWidth: 150, title: '借入商品',style:"font-size:12px;", templet: function(d){
                            return genGoodsAndPacket(d.borrowInGoodsVoList);
                        }},
                    {width: 150, title: '预计归还日期',style:"font-size:12px;",templet: function (d) {
                            return GMTToDate(d.backDate);
                        }},
                    {width: 120, title: '入库门店',style:"font-size:12px;", templet: function (d) {
                            return d.houseVo == null ? "" :d.houseVo.store.name;
                        }},
                    {width: 120, title: '入库仓库',style:"font-size:12px;", templet: function (d) {
                            return d.houseVo == null ? "" :d.houseVo.name;
                        }},
                    {width: 150, title: '当前状态',style:"font-size:12px;",templet: function (d) {
                            if (d.inStatus == 1) {
                                return '未归还';
                            } else if (d.inStatus == 2) {
                                return '部分归还';
                            } else {
                                return '已归还';
                            }
                        }},
                    {title: '操作', minWidth: 80,fixed:'right', templet: '#operBar'},
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                },
                done: function(res, curr, count){
                    setTableLineCss("table",res.data, "close", true, "font-gray-c2c2c2");
                }
            });
        }

        function close(data) {
            if (data.close){
                tip("订单已关闭");
                return;
            }
            layer.confirm('关闭单据《'+data.no+'》?', {icon: 3, title:'关闭单据'}, function(index){
                tipLoad("关闭中..");
                post("${baseUrl}house/borrowIn/close.action",{id:data.id}, function (resp) {
                    closeAll();
                    if (resp.code == 1){
                        tip("关闭成功");
                        refreshMainIframe("${baseUrl}house/borrowIn.action");
                        renderTable();
                    } else {
                        errTip(resp.msg);
                    }
                });
            });
        }

        function exportExcel(data) {
            confirm("是否导出选择的单据?",function () {
                tipLoad("处理中，请稍后");
                post("${baseUrl}house/borrowIn/export.action" + genParam(genSearch()),{},function (resp) {
                    closeAll();
                    if (resp.code == 1) {
                        downloadFileByForm("${baseUrl}file/excel/temp/"+resp.data);
                    }  else {
                        errTip(resp.msg);
                    }
                });
            });

        }


        function genSearch() {
            let json = getFormJson($("#searchForm"));
            json.close = $('[name="noClose"]').next().hasClass("layui-form-checked") ? false:'';
            return json;
        }
    </script>

</html:header>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="table-tool-div">
            <form class="layui-form layui-form-pane" action="" id="searchForm">
                <input type="hidden" name="date1">
                <input type="hidden" name="date2">
                <input type="hidden" name="makeTime1">
                <input type="hidden" name="makeTime2">

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <input type="checkbox" name="noClose" title="不显示已关闭" lay-skin="primary" value="true" checked lay-filter="noClose">
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">其他信息</label>
                        <div class="layui-input-inline">
                            <input type="text" id="search" name="search" placeholder="编号、供应商、商品、备注" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <a class="layui-btn" lay-submit="" lay-filter="search" id="btn-search"><i class="layui-icon layui-icon-search"></i></a>
                        <a class="layui-btn" id="selectSearch">筛选</a>
                    </div>

                    <div class="table-tool-div-screen" status="close" style="display: none">
                        <div class="layui-inline">
                            <label class="layui-form-label">单据编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="no" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">商品名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="goodsName" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-inline">
                                <input type="text" name="remark" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">供应商名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="provider" class="layui-input">
                            </div>
                        </div>


                        <div class="layui-inline">
                            <label class="layui-form-label">制单日期</label>
                            <div class="layui-input-inline">
                                <input type="text" id="makeTime" placeholder="请选择范围" class="layui-input" autocomplete="off">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">入库门店</label>
                            <div class="layui-input-inline">
                                <select name="storeId"></select>
                            </div>
                        </div>


                        <div class="layui-inline">
                            <label class="layui-form-label">入库仓库</label>
                            <div class="layui-input-inline">
                                <select name="houseId"></select>
                            </div>
                        </div>

                        <div class="layui-inline" >
                            <label class="layui-form-label">经手人</label>
                            <div class="layui-input-inline">
                                <select name="userId"></select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">业务日期</label>
                            <div class="layui-input-inline">
                                <input type="text" id="date" placeholder="请选择范围" class="layui-input" autocomplete="off">
                            </div>
                        </div>

                        <div class="layui-inline" >
                            <label class="layui-form-label">制单人</label>
                            <div class="layui-input-inline">
                                <select name="makeUserId"></select>
                            </div>
                        </div>

                        <div class="layui-inline" >
                            <label class="layui-form-label">当前状态</label>
                            <div class="layui-input-inline">
                                <select name="inStatus">
                                    <option value="">全部</option>
                                    <option value="1">未归还</option>
                                    <option value="2">部分归还</option>
                                    <option value="3">已归还</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <a class="layui-btn" lay-submit="" lay-filter="search">搜索</a>
                            <a class="layui-btn layui-btn-primary" id="clearSearch">清空所有条件</a>
                        </div>
                    </div>
                    <script>
                        $("#selectSearch").click(function () {
                            if ($(".table-tool-div-screen").attr("status")=="close") {
                                $(".table-tool-div-screen").css("display","block");
                                $(".table-tool-div-screen").attr("status","open");
                                $("#search").parent().parent().css("display","none");
                                $("#search").val("");
                                $("#btn-search").css("display","none");
                                $(this).html("收起");
                            } else {
                                $(".table-tool-div-screen").css("display","none");
                                $(".table-tool-div-screen").attr("status","close");
                                $("#search").parent().parent().css("display","inline-block");
                                $("#btn-search").css("display","inline-block");
                                $(this).html("筛选");
                            }
                        });

                        $("#clearSearch").click(function () {
                            clearSearch();
                        });
                    </script>
                </div>
            </form>
        </div>
        <hr/>

        <table class="layui-hide" id="table" lay-filter="table" lay-size="sm"></table>

        <script type="text/html" id="tableToolBar">
            <div class="layui-inline">
                <a class="layui-btn" lay-event="add">新增</a>
            </div>

            <div class="layui-inline">
                <a class="layui-btn layui-btn-primary" lay-event="export">导出</a>
            </div>
        </script>

        <script type="text/html" id="operBar">
            {{# if(d.inStatus!=3){ }}
            <a class="layui-btn layui-btn-xs table-operator" lay-event="back">归还</a>
            <a class="layui-btn layui-btn-xs layui-btn-warm table-operator" lay-event="inGoods">转进货</a>
            {{# } }}
            <a class="layui-btn layui-btn-xs layui-btn-normal table-operator" lay-event="detail">详情</a>

            {{#  if((_login_user.cancelOtherOrder && !d.close) || _login_user.id==d.userId){ }}
            <a class="layui-btn layui-btn-xs layui-btn-danger table-operator" lay-event="close">作废</a>
            {{#  }  }}
        </script>
    </div>
</div>
</body>
<html:loading/>
</html>